﻿#region License
/* ---------------------------------------------------------------- *
 *
 * This file is part of the Xcoordination Application Space
 * ("XcoAppSpace") http://www.xcoordination.com
 *
 * Copyright (C) 2009 Xcoordination http://www.xcoordination.com
 *
 * XcoAppSpace is free software; you can redistribute it and/or
 * modify it under the terms of version 2 of the GNU General
 * Public License as published by the Free Software Foundation.
 *
 * XcoAppSpace is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see http://www.gnu.org/licenses/
 * or write to the Free Software Foundation, Inc., 51 Franklin
 * Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * ---------------------------------------------------------------- */
#endregion

using System;
using System.Collections.Generic;
using XcoAppSpaces.Contracts.Service;
using XcoAppSpaces.Contracts.Exceptions;

namespace XcoAppSpaces.Transport.Jabber
{
    public class ConfigStringInterpreter : XcoConfigStringInterpreterBase
    {
        public const string ConfigJid = "jabber.jid";
        public const string ConfigPassword = "jabber.password";
        public const string ConfigHostname = "jabber.hostname";
        public const string ConfigPriority = "jabber.priority";
        public const string ConfigName = "jabber.name";
        public const string ConfigIsDefault = "jabber.isdefault";


        private string _Jid;
        private string _Password;
        private string _Hostname;
        private int? _Priority;
        private string _serviceName;
        private bool? _isdefault;
        /// <summary>
        /// Action definition for interpretion
        /// </summary>
        public override Dictionary<string, Action<string>> InterpretDefinition
        {
            get
            {
                return new Dictionary<string, Action<string>> 
                    { 
                        {ConfigJid, AnalyseJid},
                        {ConfigHostname, AnalyseHostname},
                        {ConfigPriority, AnalysePriority},
                        {ConfigPassword, AnalysePassword},
                        {ConfigName, AnalyseName},//Configuration key for setting the name.
                        {ConfigIsDefault, AnalyseIsDefault},//Configuration key for setting if the service is the default communication service.
                    };
            }
        }

        /// <summary>
        /// Get the current tcp service
        /// </summary>
        public override IXcoService GetService
        {
            get
            {
                return AnalyseConfig();
            }
        }

        #region analyse methods

        void AnalyseJid(string jid)
        {
            _Jid = jid;
        }
        void AnalysePassword(string password)
        {
            _Password = password;
        }
        void AnalyseHostname(string hostname)
        {
            _Hostname = hostname;
        }
        void AnalysePriority(string priorityStr)
        {
            int priority;
            if (int.TryParse(priorityStr, out priority))
            { //try to load the priority-definition from the configuration
                _Priority = priority;
            }
            else
            {
                throw new XcoConfigurationException("Priority must be a number");
            }
        }

        void AnalyseName(string name)
        {
            _serviceName = name;
        }

        void AnalyseIsDefault(string isDefaultStr)
        {
            bool isDefault;
            if (bool.TryParse(isDefaultStr, out isDefault))
            { //try to load the isDefault-definition from the configuration
                _isdefault = isDefault;
            }
            else
            {
                throw new XcoConfigurationException("IsDefault must be either \"true\" or \"false\"!");
            }
        }
        #endregion

        private XcoJabberTransportService AnalyseConfig()
        {
            if (!string.IsNullOrEmpty(_Jid))
            {
                if (string.IsNullOrEmpty(_Password))
                {
                    throw new XcoConfigurationException("Configuration element " + ConfigPassword +
                                                        " must be present for jabber communication service configuration!");
                }
                if (string.IsNullOrEmpty(_Hostname))
                {
                    _Hostname = "";
                }
                if (!_Priority.HasValue)
                {
                    _Priority = XcoJabberTransportService.DEFAULT_PRIORITY;
                }

                if (_isdefault.HasValue)
                {
                    ServiceConfig.IsDefault = _isdefault.Value;
                }
                if (!string.IsNullOrEmpty(_serviceName))
                {
                    ServiceConfig.Name = _serviceName;
                }

                //create service
                return new XcoJabberTransportService(_Jid, _Password, _Hostname, _Priority.Value);
            }
            return null;
        }
    }
}